 import java.util.Stack;


public class CautaRuta {
	protected final int MAX=100;
	protected Ruta rute[]=new Ruta[MAX];
	protected int nrRute=0;
	
	@SuppressWarnings("rawtypes")
	public Stack btStack=new Stack();
	
	@SuppressWarnings("rawtypes")
	public Stack btStack2=new Stack();
	
	Stack<Ruta> stack=new Stack<Ruta>();
	
	public void adaugaRuta(String p, String d, int distanta, boolean skip, boolean rutier, boolean feroviar, boolean fluvial)
	{
		if(nrRute<MAX)
		{
			Ruta z=new Ruta(p, d, distanta, skip, rutier, feroviar, fluvial);
			rute[nrRute]=z;
			nrRute++;
		}
		else 
			System.out.println("Nu mai poate fi adaugata nicio ruta!");
		
	}
	public void setup()
	{
		adaugaRuta("Arad", "Timisoara", 10, false, true, false, false);
		adaugaRuta("Timisoara", "Arad", 10, false, true, false, false);
		adaugaRuta("Timisoara", "Recas", 2, false, true, false, false);
		adaugaRuta("Recas", "Timisoara", 2, false, true, false, false);
		adaugaRuta("Recas", "Topolovat", 3, false, true, false, false);
		adaugaRuta("Topolovat", "Recas", 3, false, true, false, false);
		adaugaRuta("Recas", "Jebel", 4, false, true, false, false);
		adaugaRuta("Jebel", "Recas", 4, false, true, false, false);
		adaugaRuta("Jebel", "Moravita", 4, false, true, false, false);
		adaugaRuta("Moravita", "Jebel", 4, false, true, false, false);
		adaugaRuta("Timisoara","Moravita",  6, false, true, false, false);
		adaugaRuta("Moravita","Timisoara",  6, false, true, false, false);	
	}
	public int match(String plecare, String destinatie)
	{
		int dist=0;		
		for(int i=0;i< nrRute; i++)
		{
			if((rute[i].getLocalitatePlecare().equals(plecare) && rute[i].getLocalitateSosire().equals(destinatie))&& rute[i].isSkip()==false)
			{
				dist=rute[i].getDistanta();
			}
		}
		return dist;
	}
	public Ruta ruta(String plecare, String destinatie, int dist)
	{
		Ruta a=null;		
		for(int i=0;i< nrRute; i++)
		{
			if(rute[i].getLocalitatePlecare().equals(plecare)&& rute[i].getLocalitateSosire().equals(destinatie) && match(plecare, destinatie)==dist)
			{
				a=rute[i];
				
			}
		}
		return a;
	}
	public Ruta find(String from)
	{
		Ruta r=null;
		int i, sw=0;
		i=0;
		while(i<nrRute && sw==0 )
		{
			if(rute[i].getLocalitatePlecare().equals(from) && rute[i].isSkip()==false)
			{
				r=new Ruta(from, rute[i].getLocalitateSosire(), rute[i].getDistanta(), rute[i].isSkip(), rute[i].isFluvial(), rute[i].isRutier(), rute[i].isFeroviar());
				sw=1;
			}
			i++;
		}
		return r;
	}
	
	int sw=0;
	public Stack<Ruta> calculRuta(String from, String to)
	{
		//int sw=0;
		Ruta z=null;
		if(sw==0)
		{
				if(match(from, to)>0)
				{
					stack.push(ruta(from, to, match(from, to)));
					
					sw=1;	
					
				}
				else
				{	
					z=find(from);
					if(z!=null)
					{
						stack.push(z);
						
						if( z.getLocalitatePlecare().equals(to))
							sw=1;
						else
							calculRuta(z.getLocalitateSosire(), to);
					}
					else
					{
						Ruta rr=(Ruta)stack.pop();
						calculRuta(rr.getLocalitatePlecare(), to);
					}
					
				}
		}
		return stack;
		
	}
	


	@SuppressWarnings("unchecked")
	public void display(String from, String to)
	{
		Ruta rute;
		int dist=0;
		@SuppressWarnings("rawtypes")
		Stack stack2=new Stack();
		@SuppressWarnings("rawtypes")
		Stack stack=new Stack();
		stack2=calculRuta(from, to);
		
		while(!stack2.empty())
		{
			
			System.out.println("O ruta posibila este: ");
			rute=(Ruta)stack2.pop();
			rute.setSkip(true);
			System.out.println(rute.getLocalitateSosire()+" avem parcursa distanta: "+rute.getDistanta());
			dist+=rute.getDistanta();
			while(!stack2.empty())
			{
				stack.push(stack2.pop());
			}
			while(!stack.empty())
	        {
				rute=(Ruta)stack.pop(); 
				System.out.println(rute.getLocalitateSosire()+" avem parcursa distanta: "+rute.getDistanta());
				dist=dist+rute.getDistanta();
	        }
		
			System.out.println("Total  " + dist + " km.");
			stack2=calculRuta(from, to);
		}
		
	}
	
}